#include <iostream>
#include "utils.h"
#include <cstring>

using namespace std;

void demo_datastream()
{
	DataStream stream = DataStream(5);

	const char *ptr = "hello world";
	int len = stream.PutData(ptr, strlen(ptr));
	cout << "put data length: " << len << endl;

	cout << "is full: " << stream.isFull() << endl;

	char buf[128] = {0};
	stream.GetData(buf, 3);
	cout << buf << endl; 

	stream.GetData(buf, 3);
	cout << buf << endl; 

}

/*
 * Protocol类定义了两个数据通道：
 * Channel 1: COM(Read) <--- FIFO <-- Network(Send)
 * Channel 2: COM(Write) --> FIFO --> Network(Recv)
 * FIFO 表示数据转发的缓冲区
 */
void demo_proto()
{
	int len = 0;
	char buf[128] = {0};

	Protocol proto = Protocol(10, 10);
	proto.Clean();

	const char *data = "aaaaaaaaaaaaaaaa";

	/* channel 1 */
	len = proto.Send(data, strlen(data));
	cout << "send len: " << len << endl;

	len = proto.Read(buf, 8);
	cout << "read len: " << len << endl;
	cout << "data: " << buf << endl;
	cout << endl;

	/* channel 2 */
	memset(buf, 0, 128);
	data = "bbbbbbbbbbbbbbbb";

	len = proto.Write(data, strlen(data));
	cout << "write len: " << len << endl;

	len = proto.Recv(buf, 8);
	cout << "recv len: " << len << endl;
	cout << "data: " << buf << endl;
}

int main()
{
	//demo_datastream();

	demo_proto();

	return 0;
}
